
package brian.huang.reader;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.io.File;
import java.io.FilenameFilter;

public class ListContentFragment extends ListFragment {
    public static final String TAG_EPUB = "list_epub";
    public static final String TAG_TEXT = "list_text";
    public static final String TAG_HISTORY = "list_history";
    String mTag;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mTag = getTag();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ListAdapter adapter;

        if (TAG_EPUB.equals(mTag)) {
            adapter = listFiles(getActivity(), "/mnt/sdcard/Download", "epub");
        } else if (TAG_TEXT.equals(mTag)) {
            adapter = listFiles(getActivity(), "/mnt/sdcard", "txt");
        } else if (TAG_HISTORY.equals(mTag)) {
            adapter = listFiles(getActivity(), "/mnt/sdcard", "txt");
        } else {
            adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_activated_1,
                    new String[] {
                        "No Data!"
                    });
        }
        setListAdapter(adapter);

        ListView lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        getListView().setItemChecked(position, true);
        ArrayAdapter<ViewItem> adapter = (ArrayAdapter<ViewItem>) getListAdapter();

        ReaderFragment reader = new ReaderFragment();
        String uri = adapter.getItem(position).getUri().toString();

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.detail, reader, uri);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
    }

    private ListAdapter listFiles(Context context, String directory, final String ext) {
        ArrayAdapter<ViewItem> adapter = new ArrayAdapter<ViewItem>(context,
                android.R.layout.simple_list_item_activated_1);
        File folder = new File(directory);
        File[] files = folder.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String filename) {
                if (filename.endsWith("." + ext)) {
                    return true;
                }
                return false;
            }
        });
        for (File file : files) {
            adapter.add(new ViewItem(context, file));
        }

        return adapter;
    }
}
